package pl.oczadly.jabberclient.login.view;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.BoxLayout;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JPasswordField;
import javax.swing.JTextField;

import pl.oczadly.jabberclient.login.presenter.LoginPresenter;
import pl.oczadly.jabberclient.mainwindow.view.IMainView;
import pl.oczadly.jabberclient.mainwindow.view.MainView;
import pl.oczadly.jabberclient.utils.Fonts;
import pl.oczadly.jabberclient.utils.Utils;

public class LoginView implements ILoginView {
	
	private JTextField loginTextField;
	private JPasswordField passwordTextField;
	private JFrame frame;
	private LoginPresenter presenter;
	
	public void createUI() {
		frame = new JFrame( Utils.LOGIN_TITLE );

		addLayout( frame );
		
		frame.setSize( 280, 150 );
		frame.setVisible( true );
		frame.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE );
	}

	public String getLogin() {
		return loginTextField.getText();
	}
	
	public String getPassword() {
		char[] password = passwordTextField.getPassword();
		
		String passwordString = "";
		
		for( char c : password ) {
			passwordString += c;
		}
		
		return passwordString;
	}
	
	public void close() {
		frame.dispose();
	}
	
	public void sayHello() {
		JOptionPane.showMessageDialog( null, Utils.HELLO + getLogin() + "\n" + getPassword() );
	}

	private void addLayout( JFrame frame ) {
		JPanel mainPanel = new JPanel();
		
		BoxLayout boxLayout = new BoxLayout( mainPanel, BoxLayout.Y_AXIS );
		mainPanel.setLayout( boxLayout );
		
		JLabel logLabel = new JLabel( Utils.LOG_LABEL );
		logLabel.setFont( Fonts.ACCOUNT_FONT );
		
		JPanel loginPanel = new JPanel();
		JLabel loginLabel = new JLabel( Utils.LOGIN_LABEL );
		loginTextField = new JTextField( 20 );
		
		loginPanel.add( loginLabel );
		loginPanel.add( loginTextField );
		
		JPanel passwordPanel = new JPanel();
		JLabel passwordLabel = new JLabel( Utils.PASSWORD_LABEL );
		passwordTextField = new JPasswordField( 20 );
		passwordTextField.addActionListener( new LoginActionListener() );
		
		passwordPanel.add( passwordLabel );
		passwordPanel.add( passwordTextField );
		
		JPanel buttonPanel = new JPanel();
		JButton loginButton = new JButton( Utils.LOGIN_BUTTON );
		loginButton.addActionListener( new LoginActionListener() );
		
		buttonPanel.add( loginButton );
		
		mainPanel.add( logLabel );
		mainPanel.add( loginPanel );
		mainPanel.add( passwordPanel );
		mainPanel.add( buttonPanel );
		
		frame.add( mainPanel );
	}

	private class LoginActionListener implements ActionListener {

		public void actionPerformed( ActionEvent e ) {
			presenter = new LoginPresenter();
			presenter.signIn( getLogin(), getPassword() );
			
			close();
			sayHello();
			
			IMainView mainView = new MainView();
			mainView.createUI();
		}
		
	}

}
